/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.unesp.demac.bcc.soo.util;

import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JDesktopPane;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author aluno
 */
public class FrameUtil {

    private FrameUtil() {
    }

    public static void centralizar(JFrame f) {
        Dimension screenSize =
                Toolkit.getDefaultToolkit().getScreenSize();
        f.setLocation(
                (screenSize.width - f.getWidth()) / 2,
                (screenSize.height - f.getHeight()) / 2);
    }

    public static void centralizar(JDialog f) {
        Dimension screenSize =
                Toolkit.getDefaultToolkit().getScreenSize();
        f.setLocation(
                (screenSize.width - f.getWidth()) / 2,
                (screenSize.height - f.getHeight()) / 2);
    }

    public static void centralizar(JInternalFrame com, JDesktopPane desktop) {
        Dimension dx = null;
        Dimension ds = null;
        dx = desktop.getSize();
        ds = com.getSize();
        com.setLocation(((dx.width - ds.width) / 2) - desktop.WIDTH, ((dx.height - ds.height) / 2) - desktop.HEIGHT);
    }

    public static void adicionarFrame(JDesktopPane desktop, JInternalFrame tela) {
        for (JInternalFrame jInternalFrame : desktop.getAllFrames()) {
            // se uma janela semelhante estiver aberta  
            if (jInternalFrame.getClass().toString().equalsIgnoreCase(tela.getClass().toString())) {
                jInternalFrame.moveToFront(); // traz janela para frente para facilitar a escolha  

                Object[] opções = {"Utilizar Aberta", "Abrir Nova"};
                int qst = JOptionPane.showOptionDialog(null, "Abrir uma nova janela ou utilizar a que ja esta aberta?",
                        "Janela duplicada",
                        JOptionPane.DEFAULT_OPTION,
                        JOptionPane.QUESTION_MESSAGE,
                        null,
                        opções,
                        opções[0]);
                // se utilizar a aberta retorna e não abre outra,  
                // caso contrário sai do for e abre outra igual  
                if (qst == 0) {
                    return;
                } else if (qst == 1) {
                    break;
                }
            }
        }

        desktop.add(tela); //adiciona na tela  
        tela.setVisible(true); // seta visivel 
        centralizar(tela, desktop);
    }

    public static void setLook(JFrame f) {
        final String lfname = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";

        try {
            UIManager.setLookAndFeel(lfname);
        } catch (ClassNotFoundException ex) {
            ex.getMessage();
        } catch (IllegalAccessException ex) {
            ex.getMessage();
        } catch (InstantiationException ex) {
            ex.getMessage();
        } catch (UnsupportedLookAndFeelException ex) {
            ex.getMessage();
        }

        SwingUtilities.updateComponentTreeUI(f);
    }
}
